﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Edit_F4CheckFORS300 : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        private DataSet ds;
        private DataViewManager dsView;

        private Dictionary<string, string> required_list;

        public string serial;

        public frm_Edit_F4CheckFORS300()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);

            this.serial = null;
            this.ds = new DataSet("RobotOrderDetail");

            this.required_list = new Dictionary<string, string>() { { "serialNo", "Serial No" }, { "writtenBy", "Written By" },
            { "checkBy", "Check By"}, {"inputDate", "Date"}, {"wiringDiagram", "Wiring Diagram"}, {"magrinY", "Current Magrin Y"}, 
            {"magrinZ", "Current Magrin Z"}, {"pcPower", "PC Power"}, {"pcPowerSerial", "PC Power Serial"}, {"rd023Y", "RD023-Y"},
            {"yRun", "Y Run"}, {"yStop", "Y Stop"}, {"rd023Z", "RD023-Z"}, {"zRun", "Z Run"}, {"zStop", "Z Stop"}, {"rc4201", "RC420-1"}, 
            {"rc4201Ver", "RC420-1-Ver"}, {"rc4202", "RC420-2"}, {"rc4202Ver", "RC420-2-Ver"}, {"rc4203", "RC420-3"}, 
            {"rc4203Ver", "RC420-3-Ver"}, {"rc4204", "RC420-4"}, {"rc4204Ver", "RC420-4-Ver"}, {"rc4205", "RC420-5"}, 
            {"rc4205Ver", "RC420-5-Ver"}, {"rc4206", "RC420-6"}, {"rc4206Ver", "RC420-6-Ver"}, {"y1", "Y1"}, {"y2", "Y2"}, {"y3", "Y3"},
            {"y4", "Y4"}, {"y5", "Y5"}, {"y6", "Y6"}, {"z1", "Z1"}, {"z2", "Z2"}, {"z3", "Z3"}, {"z4", "Z4"}, {"z5", "Z5"}, {"z6", "Z6"},
            {"mappingTop", "Mapping Top"}, {"mappingBottom", "Mapping Bottom"}, {"ipAddress", "IP Address"},
            {"cfImage", "CF Image"}, {"hostName", "Host Name"}, {"hostIPAddr", "Host IP Address"}, {"bodyNo", "Body No"},
            {"pcBoard", "PC Board"}, {"bcr", "BRC"}, {"remark", "Remark"}, {"appDiagram", "Appearance Diagram"},
            {"stackerThres", "Stacker Hand Threshold"}, {"mapperThres", "Mapper Threshold"}, {"laserThres", "Laser Prt Threshold"}};

            LoadStaffList();
            LoadWiring();
        }

        ////////////////////////////////////////////////////
        /// Load Serial List
        ////////////////////////////////////////////////////
        private void LoadSerialList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo], [CompID] FROM [231_RobotOrderDetail]"
                    + " WHERE(FinishedDate > DateAdd(dd, 0, DateDiff(dd, 0, GETDATE())) - 500) AND [SerialNo] NOT IN"
                    + " (SELECT [SerialNo] COLLATE Vietnamese_CI_AS FROM [231_F4RV201] WHERE [SerialNo] <> '" + this.serial + "')";
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "RobotOrderDetail");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.serialNo.DataSource = dsView;
                this.serialNo.DisplayMember = "RobotOrderDetail.SerialNo";
                this.serialNo.ValueMember = "RobotOrderDetail.SerialNo";
                this.serialNo.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.partNo.DataBindings.Add("Text", dsView, "RobotOrderDetail.CompID");
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }
        
        /// <summary>
        /// Load Staff List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadStaffList()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT StaffID, StaffName FROM [222_Staff] WHERE StopDate IS NULL ORDER BY StaffID";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    string[] fullname = dataReader["StaffName"].ToString().Trim().Split(' ');

                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                    TextInfo textInfo = cultureInfo.TextInfo;
                    string txt = textInfo.ToTitleCase(fullname[fullname.Length - 1].ToLower());

                    item.Text = txt + dataReader["StaffID"].ToString().Trim();
                    item.Value = txt + dataReader["StaffID"].ToString().Trim();

                    this.writtenBy.Properties.Items.Add(item);
                    this.checkBy.Properties.Items.Add(item);
                }
                dataReader.Close();
                sql_cmd.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        ////////////////////////////////////
        /// Load Wiring Diagram
        ////////////////////////////////////
        private void LoadWiring()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT [WiringDiagram] FROM [231_F4RV201Default]";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["WiringDiagram"].ToString().Trim();
                    item.Value = dataReader["WiringDiagram"].ToString().Trim();
                    this.wiringDiagram.Properties.Items.Add(item);
                }
                dataReader.Close();
                sql_cmd.Dispose();

                sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT [SerialNo] FROM [231_F5Detail]"
                    + " WHERE ([FinishDate] > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) - 500)";
                dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["SerialNo"].ToString().Trim();
                    item.Text = dataReader["SerialNo"].ToString().Trim();

                    this.pcPowerSerial.Properties.Items.Add(item);
                    this.rd023Y.Properties.Items.Add(item);
                    this.rd023Z.Properties.Items.Add(item);
                    this.rc4201.Properties.Items.Add(item);
                    this.rc4202.Properties.Items.Add(item);
                    this.rc4103.Properties.Items.Add(item);
                    this.rc4104.Properties.Items.Add(item);
                    this.rc4105.Properties.Items.Add(item);
                    this.rc4106.Properties.Items.Add(item);
                }
                dataReader.Close();
                sql_cmd.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Load Data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(this.serial))
            {
                ret = false;
            }
            else
            {
                LoadSerialList();
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT t1.[SerialNo], t1.[Written], t1.[Date], t1.[Checked],"
                        + " t1.[WiringDaiagram], Y, Z, t1.[PCPower], t1.[PCPowerSerial], t1.[RC420_1], t1.[RC420_1_Ver],"
                        + " t1.[RC420_2], t1.[RC420_2_Ver], t1.[RC410_3], t1.[RC410_3_Ver], t1.[RC410_4], t1.[RC410_4_Ver],"
                        + " t1.[RC410_5], t1.[RC410_5_Ver], t1.[RC410_6], t1.[RC410_6_Ver], t1.[RD023_Y], [Y_Run],"
                        + " t1.[Y_Stop], t1.[RD023_Z], t1.[Z_Run], t1.[Z_Stop], t1.[Y1], t1.[Y2], t1.[Y3], t1.[Y4],"
                        + " t1.[Y5], t1.[Y6], t1.[Z1], t1.[Z2], t1.[Z3], t1.[Z4], t1.[Z5], t1.[Z6], t1.[Top],"
                        + " t1.[Bottom], [Ver], t1.[IP_Adress], t1.[PC_Board], t1.[BCR], t1.[Host name], t1.[Host IP Adrs],"
                        + " t1.[Body No], t1.[Remark], t1.[BCR data], t1.[CF Image], t1.[Appearance Diagram],"
                        + " t1.[Mapper Threshold], t1.[Stacker Hand Threshold], t1.[Laser Prt Threshold], t2.[CompID]"
                        + " FROM [231_F4RV201] AS t1 LEFT JOIN"
                        + " [231_RobotOrderDetail] AS t2 ON t1.[SerialNo] COLLATE Vietnamese_CI_AS=t2.[SerialNo]"
                        + " WHERE t1.[SerialNo]='" + this.serial + "'";
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        if (this.serialNo.FindString(serial) > 0)
                        {
                            this.serialNo.SelectedIndex = this.serialNo.FindString(serial);
                        }
                        for (int i = 0; i < this.writtenBy.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.writtenBy.Properties.Items[i];
                            if (item.Value.ToString().ToUpper() == dataReader["Written"].ToString().Trim().ToUpper())
                            {
                                this.writtenBy.SelectedIndex = i;
                            }
                        }
                        this.inputDate.EditValue = DateTime.Parse(dataReader["Date"].ToString().Trim());
                        for (int i = 0; i < this.checkBy.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.checkBy.Properties.Items[i];
                            if (item.Value.ToString().ToUpper() == dataReader["Checked"].ToString().Trim().ToUpper())
                            {
                                this.checkBy.SelectedIndex = i;
                            }
                        }
                        for (int i = 0; i < this.wiringDiagram.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.wiringDiagram.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["WiringDaiagram"].ToString().Trim())
                            {
                                this.wiringDiagram.SelectedIndex = i;
                            }
                        }                            
                        this.magrinY.Text = dataReader["Y"].ToString().Trim();
                        this.magrinZ.Text = dataReader["Z"].ToString().Trim();
                        this.pcPower.Text = dataReader["PCPower"].ToString().Trim();                        
                        for (int i = 0; i < this.pcPowerSerial.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.pcPowerSerial.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["PCPowerSerial"].ToString().Trim())
                            {
                                this.pcPowerSerial.SelectedIndex = i;
                            }
                        }
                        for (int i = 0; i < this.rc4201.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.rc4201.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RC420_1"].ToString().Trim())
                            {
                                this.rc4201.SelectedIndex = i;
                            }
                        }                        
                        this.rc4201Ver.Text = dataReader["RC420_1_Ver"].ToString().Trim();
                        
                        for (int i = 0; i < this.rc4202.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.rc4202.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RC420_2"].ToString().Trim())
                            {
                                this.rc4202.SelectedIndex = i;
                            }
                        }
                        this.rc4202Ver.Text = dataReader["RC420_2_Ver"].ToString().Trim();

                        for (int i = 0; i < this.rc4103.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.rc4103.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RC410_3"].ToString().Trim())
                            {
                                this.rc4103.SelectedIndex = i;
                            }
                        }
                        this.rc4103Ver.Text = dataReader["RC410_3_Ver"].ToString().Trim();
                        
                        for (int i = 0; i < this.rc4104.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.rc4104.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RC410_4"].ToString().Trim())
                            {
                                this.rc4104.SelectedIndex = i;
                            }
                        }
                        this.rc4104Ver.Text = dataReader["RC410_4_Ver"].ToString().Trim();
                        for (int i = 0; i < this.rc4105.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.rc4105.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RC410_5"].ToString().Trim())
                            {
                                this.rc4105.SelectedIndex = i;
                            }
                        }
                        this.rc4105Ver.Text = dataReader["RC410_5_Ver"].ToString().Trim();
                        for (int i = 0; i < this.rc4106.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.rc4106.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RC410_6"].ToString().Trim())
                            {
                                this.rc4106.SelectedIndex = i;
                            }
                        }
                        this.rc4106Ver.Text = dataReader["RC410_6_Ver"].ToString().Trim();
                        for (int i = 0; i < this.rd023Y.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.rd023Y.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RD023_Y"].ToString().Trim())
                            {
                                this.rd023Y.SelectedIndex = i;
                            }
                        }
                        for (int i = 0; i < this.rd023Z.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.rd023Z.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RD023_Z"].ToString().Trim())
                            {
                                this.rd023Z.SelectedIndex = i;
                            }
                        }
                        this.yRun.Text = dataReader["Y_Run"].ToString().Trim();
                        this.yStop.Text = dataReader["Y_Stop"].ToString().Trim();
                        this.zRun.Text = dataReader["Z_Run"].ToString().Trim();
                        this.zStop.Text = dataReader["Z_Stop"].ToString().Trim();
                        this.y1.Text = dataReader["Y1"].ToString().Trim();
                        this.y2.Text = dataReader["Y2"].ToString().Trim();
                        this.y3.Text = dataReader["Y3"].ToString().Trim();
                        this.y4.Text = dataReader["Y4"].ToString().Trim();
                        this.y5.Text = dataReader["Y5"].ToString().Trim();
                        this.y6.Text = dataReader["Y6"].ToString().Trim();
                        this.z1.Text = dataReader["Z1"].ToString().Trim();
                        this.z2.Text = dataReader["Z2"].ToString().Trim();
                        this.z3.Text = dataReader["Z3"].ToString().Trim();
                        this.z4.Text = dataReader["Z4"].ToString().Trim();
                        this.z5.Text = dataReader["Z5"].ToString().Trim();
                        this.z6.Text = dataReader["Z6"].ToString().Trim();
                        this.mappingTop.Text = dataReader["Top"].ToString().Trim();
                        this.mappingBottom.Text = dataReader["Bottom"].ToString().Trim();
                        this.softVer.Text = dataReader["Ver"].ToString().Trim();
                        this.ipAddress.Text = dataReader["IP_Adress"].ToString().Trim();
                        this.pcBoard.Text = dataReader["PC_Board"].ToString().Trim();
                        this.bcr.Text = dataReader["BCR"].ToString().Trim();
                        this.hostName.Text = dataReader["Host name"].ToString().Trim();
                        this.hostIPAddr.Text = dataReader["Host IP Adrs"].ToString().Trim();
                        this.bodyNo.Text = dataReader["Body No"].ToString().Trim();
                        this.remark.Text = dataReader["Remark"].ToString().Trim();
                        this.bcrData.Text = dataReader["BCR data"].ToString().Trim();
                        this.cfImage.Text = dataReader["CF Image"].ToString().Trim();
                        this.appDiagram.Text = dataReader["Appearance Diagram"].ToString().Trim();
                        this.mapperThres.Text = dataReader["Mapper Threshold"].ToString().Trim();
                        this.stackerThres.Text = dataReader["Stacker Hand Threshold"].ToString().Trim();
                        this.laserThres.Text = dataReader["Laser Prt Threshold"].ToString().Trim();                        
                    }
                    sql_cmd.Dispose();
                    myConnection.Close();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                    //MessageBox.Show(ex.Message);
                }
            }
            return ret;
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;
            if (this.serialNo.SelectedIndex < 0)
            {
                error = "Please select Serial No.";
            }
            if (string.IsNullOrEmpty(error))
            {
                foreach (Control c in this.Controls)
                {
                    if (c is System.Windows.Forms.ComboBox)
                    {
                        if (((System.Windows.Forms.ComboBox)c).SelectedIndex < 0)
                        {
                            if (required_list.ContainsKey(((System.Windows.Forms.ComboBox)c).Name))
                            {
                                error = "Please select " + required_list[((System.Windows.Forms.ComboBox)c).Name];
                            }
                        }
                    }

                    if (c is DevExpress.XtraEditors.ComboBoxEdit)
                    {
                        if (((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex < 0)
                        {
                            if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)c).Name))
                            {
                                error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)c).Name];
                            }
                        }
                    }

                    if (c is DevExpress.XtraEditors.TextEdit)
                    {
                        if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)c).Text.Trim()))
                        {
                            if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)c).Name))
                            {
                                error = "Please enter " + required_list[((DevExpress.XtraEditors.TextEdit)c).Name];
                            }
                        }
                    }

                    if (c is System.Windows.Forms.TextBox)
                    {
                        if (string.IsNullOrEmpty(((System.Windows.Forms.TextBox)c).Text.Trim()))
                        {
                            if (required_list.ContainsKey(((System.Windows.Forms.TextBox)c).Name))
                            {
                                error = "Please enter " + required_list[((System.Windows.Forms.TextBox)c).Name];
                            }
                        }
                    }

                    if (c is System.Windows.Forms.GroupBox)
                    {
                        foreach (Control sub_c in c.Controls)
                        {
                            if (sub_c is DevExpress.XtraEditors.TextEdit)
                            {
                                if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)sub_c).Text.Trim()))
                                {
                                    if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)sub_c).Name))
                                    {
                                        error = "Please enter " + required_list[((DevExpress.XtraEditors.TextEdit)sub_c).Name];
                                    }
                                }
                            }

                            if (sub_c is DevExpress.XtraEditors.ComboBoxEdit)
                            {
                                if (((DevExpress.XtraEditors.ComboBoxEdit)sub_c).SelectedIndex < 0)
                                {
                                    if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Name))
                                    {
                                        error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Name];
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                DateTime date = DateTime.Parse(this.inputDate.EditValue.ToString());
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "UPDATE [231_F4RV201] SET"
                        + " [SerialNo]='" + this.serialNo.SelectedValue.ToString() + "',"
                        + " [Written]='" + this.writtenBy.Text.Trim() + "',"
                        + " [Date]=@date, [Checked]='" + this.checkBy.Text.Trim() + "',"
                        + " [WiringDaiagram]='" + this.wiringDiagram.Text.Trim() + "',"
                        + " [PCPower]='" + this.pcPower.Text.Trim() + "',"
                        + " [PCPowerSerial]='" + this.pcPowerSerial.Text.Trim() + "',"
                        + " [RC420_1]='" + this.rc4201.Text.Trim() + "', [RC420_1_Ver]='" + this.rc4201Ver.Text.Trim() + "',"
                        + " [RC420_2]='" + this.rc4202.Text.Trim() + "', [RC420_2_Ver]='" + this.rc4202Ver.Text.Trim() + "',"
                        + " [RC410_3]='" + this.rc4103.Text.Trim() + "', [RC410_3_Ver]='" + this.rc4103Ver.Text.Trim() + "',"
                        + " [RC410_4]='" + this.rc4104.Text.Trim() + "', [RC410_4_Ver]='" + this.rc4104Ver.Text.Trim() + "',"
                        + " [RC410_5]='" + this.rc4105.Text.Trim() + "', [RC410_5_Ver]='" + this.rc4105Ver.Text.Trim() + "',"
                        + " [RC410_6]='" + this.rc4106.Text.Trim() + "', [RC410_6_Ver]='" + this.rc4106Ver.Text.Trim() + "',"
                        + " [RD023_Y]='" + this.rd023Y.Text.Trim() + "', [Y_Run]='" + this.yRun.Text.Trim() + "',"
                        + " [Y_Stop]='" + this.yStop.Text.Trim() + "', [RD023_Z]='" + this.rd023Z.Text.Trim() + "',"
                        + " [Z_Run]='" + this.zRun.Text.Trim() + "', [Z_Stop]='" + this.zStop.Text.Trim() + "',"
                        + " [Y1]='" + this.y1.Text.Trim() + "', [Y2]='" + this.y2.Text.Trim() + "',"
                        + " [Y3]='" + this.y3.Text.Trim() + "', [Y4]='" + this.y4.Text.Trim() + "',"
                        + " [Y5]='" + this.y5.Text.Trim() + "', [Y6]='" + this.y6.Text.Trim() + "',"
                        + " [Z1]='" + this.z1.Text.Trim() + "', [Z2]='" + this.z2.Text.Trim() + "',"
                        + " [Z3]='" + this.z3.Text.Trim() + "', [Z4]='" + this.z4.Text.Trim() + "',"
                        + " [Z5]='" + this.z5.Text.Trim() + "', [Z6]='" + this.z6.Text.Trim() + "',"
                        + " [Top]='" + this.mappingTop.Text.Trim() + "', [Bottom]='" + this.mappingBottom.Text.Trim() + "',"
                        + " [IP_Adress]='" + this.ipAddress.Text.Trim() + "', [PC_Board]='" + this.pcBoard.Text.Trim() + "',"
                        + " [BCR]='" + this.bcr.Text.Trim() + "', [Host name]='" + this.hostName.Text.Trim() + "',"
                        + " [Host IP Adrs]='" + this.hostIPAddr.Text.Trim() + "', [Body No]='" + this.bodyNo.Text.Trim() + "',"
                        + " [Remark]='" + this.remark.Text.Trim() + "', [BCR data]='" + this.bcrData.Text.Trim() + "',"
                        + " [CF Image]='" + this.cfImage.Text.Trim() + "', [Appearance Diagram]='" + this.appDiagram.Text.Trim() + "',"
                        + " [Mapper Threshold]='" + this.mapperThres.Text.Trim() + "',"
                        + " [Stacker Hand Threshold]='" + this.stackerThres.Text.Trim() + "',"
                        + " [Laser Prt Threshold]='" + this.laserThres.Text.Trim() + "'"
                        + " WHERE [SerialNo] = '" + this.serial + "'";
                    sql_cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = date.Date;
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Database updated successfully!");
                    this.Dispose();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                }                
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void serialNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.serialNo.Text.Substring(this.serialNo.Text.Length - 1, 1).ToUpper() == "F")
            {
                this.TSerialNo.Text = this.serialNo.Text.Substring(0, this.serialNo.Text.Length - 1);
            }
            else
            {
                this.TSerialNo.Text = this.serialNo.Text;
            }
        }

    }
}
